import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../models/complaint.dart';
import '../widgets/complaint_card.dart';
import 'complaint_page.dart';
import 'complaint_detail_page.dart';
import 'admin_login_page.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final controller = Get.find<ComplaintController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('随手拍'),
        actions: [
          IconButton(
            icon: const Icon(Icons.refresh),
            onPressed: controller.loadComplaints,
          ),
        ],
      ),
      body: Column(
        children: [
          Container(
            width: double.infinity,
            padding: const EdgeInsets.all(16),
            alignment: Alignment.center,
            color: Theme.of(context).primaryColor.withOpacity(0.1),
            child: const Text(
              '生态环境投诉记录',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
          ),
          Obx(
            () => Expanded(
              child:
                  controller.isLoading.value
                      ? const Center(child: CircularProgressIndicator())
                      : controller.complaints.isEmpty
                      ? Center(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            const Icon(
                              Icons.inbox,
                              size: 64,
                              color: Colors.grey,
                            ),
                            const SizedBox(height: 16),
                            const Text(
                              '暂无投诉记录',
                              style: TextStyle(
                                fontSize: 16,
                                color: Colors.grey,
                              ),
                            ),
                          ],
                        ),
                      )
                      : RefreshIndicator(
                        onRefresh: controller.loadComplaints,
                        child: ListView.builder(
                          padding: const EdgeInsets.all(16),
                          itemCount: controller.complaints.length,
                          itemBuilder: (context, index) {
                            final complaint = controller.complaints[index];
                            return Padding(
                              padding: const EdgeInsets.only(bottom: 16),
                              child: ComplaintCard(
                                complaint: complaint,
                                onTap:
                                    () => Get.to(
                                      () => ComplaintDetailPage(
                                        complaint: complaint,
                                      ),
                                    ),
                              ),
                            );
                          },
                        ),
                      ),
            ),
          ),
          Container(
            padding: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.1),
                  blurRadius: 4,
                  offset: const Offset(0, -2),
                ),
              ],
            ),
            child: Row(
              children: [
                Expanded(
                  child: ElevatedButton.icon(
                    onPressed: () => Get.to(() => const AdminLoginPage()),
                    icon: const Icon(Icons.admin_panel_settings),
                    label: const Text('管理员登录'),
                    style: ElevatedButton.styleFrom(
                      padding: const EdgeInsets.symmetric(vertical: 12),
                    ),
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: ElevatedButton.icon(
                    onPressed: () => Get.to(() => const ComplaintPage()),
                    icon: const Icon(Icons.add_a_photo),
                    label: const Text('我要投诉'),
                    style: ElevatedButton.styleFrom(
                      padding: const EdgeInsets.symmetric(vertical: 12),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
